package com.example.kafka;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * ClassName: MyPartitioner
 * Package: com.example.kafka
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-07-30
 * Time: 16:56
 */

//自定义分区
public class MyPartitioner implements Partitioner {


    // 参数分别是 主题 key  byte[] KeyBytes 序列化之后的key 和 val
    @Override
    public int partition(String topic, Object key, byte[] KeyBytes, Object value, byte[] ValBytes1, Cluster cluster) {
        //获取数据
        String s = value.toString();
        //定义分区
        int partition;

        if (s.contains("atguigu")){
            partition = 0;
        }else{
            partition = 1;
        }
        return partition;
    }

    @Override
    public void close() {
    }
    @Override
    public void configure(Map<String, ?> map) {
    }
}
